---@diagnostic disable: undefined-field, lowercase-global

PINEA_EVENT_QUIT = 0x100
PINEA_EVENT_KEY_DOWN = 0x300
PINEA_EVENT_KEY_UP = 0x301
PINEA_EVENT_MOUSE_MOTION = 0x400
PINEA_EVENT_MOUSE_BUTTON_DOWN = 0x401
PINEA_EVENT_MOUSE_BUTTON_UP = 0x402
PINEA_EVENT_MOUSE_WHEEL = 0x403

PINEA_KEY_SPACE = 0x00000020
PINEA_KEY_A = 0x00000061
PINEA_KEY_D = 0x00000064
PINEA_KEY_S = 0x00000073
PINEA_KEY_W = 0x00000077
PINEA_KEY_H = 0x00000068
PINEA_KEY_J = 0x0000006a
PINEA_KEY_K = 0x0000006b
PINEA_KEY_L = 0x0000006c

PINEA_FLIP_HORIZONTAL = 0x00000001
PINEA_FLIP_VERTICAL = 0x00000002

function PineaRect(x, y, w, h)
    return {
        x = x,
        y = y,
        w = w,
        h = h
    }
end

function PineaColor(r, g, b, a)
    return {
        r = r,
        g = g,
        b = b,
        a = a
    }
end

function PineaEvent()
    return {
        type = 0,
        key = 0,
        x = 0,
        y = 0
    }
end

function pineaInit(title, width, height)
    return _G.l_pineaInit(title, width, height)
end

function pineaQuit()
    return _G.l_pineaQuit()
end

function pineaShow()
    return _G.l_pineaShow()
end

function pineaPollEvent(event)
    local r, e = _G.l_pineaPollEvent(event)
    if r then
        event.type = e.type
        event.key = e.key
        event.x = e.x
        event.y = e.y
    end
    return r
end

function pineaClear(color)
    return _G.l_pineaClear(color)
end

function pineaRender()
    return _G.l_pineaRender()
end

function pineaFillRect(rect, color)
    return _G.l_pineaFillRect(rect, color)
end

function pineaLoadImage(path)
    return _G.l_pineaLoadImage(path)
end

function pineaDrawImage(image, srcRect, dstRect, angle, flip)
    return _G.l_pineaDrawImage(image, srcRect, dstRect, angle, flip)
end

function pineaDropImage(image)
    return _G.l_pineaDropImage(image)
end

function pineaLoadMusic(path)
    return _G.l_pineaLoadMusic(path)
end

function pineaPlayMusic(audio, loop)
    return _G.l_pineaPlayMusic(audio, loop)
end

function pineaPauseMusic()
    return _G.l_pineaPauseMusic()
end

function pineaResumeMusic()
    return _G.l_pineaResumeMusic()
end

function pineaStopMusic()
    return _G.l_pineaStopMusic()
end

function pineaDropMusic(audio)
    return _G.l_pineaDropMusic(audio)
end

function pineaLoadSound(path)
    return _G.l_pineaLoadSound(path)
end

function pineaPlaySound(audio, loops)
    return _G.l_pineaPlaySound(audio, loops)
end

function pineaDropSound(audio)
    return _G.l_pineaDropSound(audio)
end

function pineaGetTime()
    return _G.l_pineaGetTime()
end

Input = {
    keys = {},
    lastKeys = {},
    update = function(key, pressed)
        Input.lastKeys[key] = Input.keys[key]
        Input.keys[key] = pressed
    end,
    isKeyPressed = function(key)
        return Input.keys[key]
    end,
    isKeyDown = function(key)
        local r = Input.keys[key] and not Input.lastKeys[key]
        if r then
            Input.lastKeys[key] = true
        end
        return r
    end,
    isKeyUp = function(key)
        local r = not Input.keys[key] and Input.lastKeys[key]
        if r then
            Input.lastKeys[key] = false
        end
        return r
    end
}
